package com.ups.singlearner.activities;

import java.util.ArrayList;
import java.util.List;

import com.ups.singlearner.AudioDetection;
import com.ups.singlearner.ComposedNote;
import com.ups.singlearner.Composition;
import com.ups.singlearner.R;
import com.ups.singlearner.util.NoteUtil;
import com.ups.singlearner.util.NoteUtil.Note;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.media.AudioFormat;
import android.media.AudioRecord;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;


public class SoundRecorderActivity extends Activity {

	/** Called when the activity is first created. */
	private int mAudioBufferSize;
	private int mAudioBufferSampleSize;
	private AudioRecord mAudioRecord;
	private boolean inRecordMode = false;
	private short[] audioBuffer;
	public static List<Note> notePlayer2 = new ArrayList<Note>();
	public static List<Note> notePlayer1 = new ArrayList<Note>();
	private Context myContext = this;
	
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.pic);
		initAudioRecord();
	}

	@Override
	public void onResume() {
		super.onResume();
		inRecordMode = true;

		Thread t = new Thread(new Runnable() {
			public void run() {
				getSamples();
			}
		});
		t.start();
	}

	protected void onPause() {
		inRecordMode = false;
		super.onPause();
	}

	@Override
	protected void onDestroy() {
		if (mAudioRecord != null) {
			mAudioRecord.release();
		}
		super.onDestroy();
	}
	
	private void initAudioRecord() {
		try {
			int sampleRate = 8000;
			int channelConfig = AudioFormat.CHANNEL_IN_MONO;
			int audioFormat = AudioFormat.ENCODING_PCM_16BIT;
			mAudioBufferSize = 2 * AudioRecord.getMinBufferSize(sampleRate,
					channelConfig, audioFormat);
			mAudioBufferSampleSize = mAudioBufferSize / 2;
			mAudioRecord = new AudioRecord(MediaRecorder.AudioSource.MIC,
					sampleRate, channelConfig, audioFormat, mAudioBufferSize);
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		}

		int audioRecordState = mAudioRecord.getState();
		if (audioRecordState != AudioRecord.STATE_INITIALIZED) {
			finish();
		}
	}

	private void getSamples() {
		if (mAudioRecord == null)
			return;
		float cpt = 5.0f;
		audioBuffer = new short[mAudioBufferSampleSize];

		mAudioRecord.startRecording();

		int audioRecordingState = mAudioRecord.getRecordingState();
		if (audioRecordingState != AudioRecord.RECORDSTATE_RECORDING) {
			finish();
		}

		//Toast.makeText(myContext, "Parlez maintenant !", Toast.LENGTH_LONG).show();
		while (inRecordMode && cpt > 0) {
			int samplesRead = mAudioRecord.read(audioBuffer, 0,
					mAudioBufferSampleSize);

			int frequence = AudioDetection.getFrequencyFFT(audioBuffer);
			Note n = NoteUtil.getNoteFromFreq(frequence, 1);
			//TODO replace 0,0,0
			Composition.getInstance().addSungNote(new ComposedNote(n, (int)(5.0-cpt)*5, 0, 0));
			cpt -=0.5f;
		}
		//result = SoundUtil.getResult(CompositionActivity.notePlayer1, notePlayer2); TODO UnComment
		//mAudioRecord.stop();
		
		/*Intent intent = new Intent(myContext, CompositionActivity.class);
		 
		startActivity(intent);*/
		finish();
	}
	
	public SoundRecorderActivity() {
		super();
		myContext = this;
	}
}